/**
 * Created by YJH on 2018/5/30.
 */

import React, {Component} from 'react';
import {
    AsyncStorage,
} from 'react-native';

const FAVORITE_KEY_PREFIX = 'favorite_';
export default class FavoriteDao {

    constructor(flag) {
        this.flag = flag;
        this.flagKey = FAVORITE_KEY_PREFIX + flag;
    }


    /**
     * 保存搜藏的项目item
     * @param key
     * @param value
     * @param callback
     */
    savaFavoriteItem(key, value) {

        AsyncStorage.setItem(key, value, error => {
            if (!error) {
                this.updateFavoriteKeys(key, true);
            }
        });
    }

    /**
     * 删除搜藏的项目item
     * @param key
     */
    removeFavoriteItem(key) {

        AsyncStorage.removeItem(key, error => {
            if (!error) {
                this.updateFavoriteKeys(key, false);
            }
        });
    }

    /**
     * 获取搜藏的项目对应的key
     * @returns {Promise}
     */
    getFavorKeys() {
        return new Promise((resolve, reject) => {
            AsyncStorage.getItem(this.flagKey, (error, result) => {
                if (!error) {
                    if (result) {
                        try {
                            resolve(JSON.parse(result));
                        } catch (err) {
                            reject(err);
                        }
                        return;
                    }
                }

                reject(JSON.parse(error))
            });
        })
    }


    /**
     * 更新增加或删除的key的集合
     * @param key
     * @param isAdd
     */
    updateFavoriteKeys(key, isAdd) {
        AsyncStorage.getItem(this.flagKey, (error, result) => {
            if (!error) {
                var favorites = [];
                if (result) {
                    favorites = JSON.parse(result);

                }
                var index = favorites.indexOf(key);
                if (isAdd) {
                    if (index === -1) {
                        favorites.push(key);
                    }
                } else {
                    if (index !== -1) {
                        favorites.splice(index, 1);
                    }
                }

                AsyncStorage.setItem(this.flagKey, JSON.stringify(favorites));
            }
        });

    }

    /**
     * 获取收藏的Repository对应的key
     * @return {Promise}
     */
    getFavoriteKeys(){
        return new Promise((resolve,reject)=>{
            AsyncStorage.getItem(this.flagKey,(error,result)=>{
                if (!error) {
                    try {
                        resolve(JSON.parse(result));
                    } catch (e) {
                        reject(error);
                    }
                }else {
                    reject(error);
                }
            });
        });
    }

    /**
     * 获取所以收藏的项目
     * @return {Promise}
     */
    getAllItems() {
        return new Promise((resolve,reject)=> {
            this.getFavoriteKeys().then((keys)=> {
                var items = [];
                if (keys) {
                    AsyncStorage.multiGet(keys, (err, stores) => {
                        try {
                            stores.map((result, i, store) => {
                                // get at each store's key/value so you can work with it
                                let key = store[i][0];
                                let value = store[i][1];
                                if (value)items.push(JSON.parse(value));
                            });
                            resolve(items);
                        } catch (e) {
                            reject(e);
                        }
                    });
                } else {
                    resolve(items);
                }
            }).catch((e)=> {
                reject(e);
            })
        })
    }
}